<?php

require FCPATH . '/application/controllers/backend/backend.php';

class Folder extends Backend {

	public function __construct() {
		parent::__construct();
		$this->load->model('node_model', 'model');
	}

	public function insert() {
		$data = array(
			'parent' => intval($this->input->post('parent')),
			'type' => 1,
			'sizes' => '',
			'name' => '',
			'label' => trim($this->input->post('label'))
		);

		if (($id = $this->model->insert($data)) > 0) {
			$this->json_back('XT001', '', array('id' => $id));
		}

		$this->json_back('XT002', '数据库或SQL语句有误。');
	}

	public function delete() {
		$id = intval($this->uri->segment(4));

		if (!($node = $this->model->find($id))) {
			$this->json_back('XT002', '该目录已被删除');
		}

		$conditions = array(
			array('where', 'parent', $id)
		);

		if ($this->model->total($conditions) > 0) {
			$this->json_back('XT002', '该目录下有文件');
		}

		if ($this->model->delete($id)) {
			$this->json_back('XT001', '删除成功');
		}
		$this->json_back('XT001', '删除失败');
	}

}
